﻿using System;
using personalplaner.business.common;
using personalplaner.business.enums;
using personalplaner.common;
using personalplaner.dataprovider;
using System.Windows.Media;
using personalplaner.business.extensions;
using System.Collections.Generic;

namespace personalplaner.servicemodel.dataaccess
{
    public interface IOptionsDataAccess
	{
		#region DisplaySettings
		
		bool ShowDaysInPlan { get; set; }

        bool ShowPensumInPlan { get; set; }

    	bool ColorWeekendInPlan { get; set; }

        bool ShowPrsId { get; set; }
        
        enNameDisplay NameDisplayOption { get; set; }

		#endregion

		#region Colors

		Color DienstCountPlusColor { get; set; }

        Color DienstCountEnoughColor { get; set; }

        Color DienstCountMinusColor { get; set; }

        Color SaturdayColor { get; set; }

        Color SundayColor { get; set; }

    	Color HolidayColor { get; set; }

		#endregion

		#region Work time settings

		double WorkTimePerDay { get; set; }

    	double WorkTimePerWeek { get; set; }

    	double HolydaysPerYear { get; set; }

    	#endregion

        #region Printing

        bool PrintSaldoDifference { get; set; }

        bool PrintSoll { get; set; }

        bool PrintHaben { get; set; }

        #endregion

        #region Person Overview

        bool ShowAbteilungName { get; set; }

        bool ShowMonth { get; set; }

        bool ShowSaldo { get; set; }

        bool ShowPlanName { get; set; }

        #endregion

        #region General

        IEnumerable<Mandant> GetMandanten();

        bool SetActiveMandant(string mandid);

        string GetActiveMandid();

        bool SavePassword { get; set; }

        bool ShowDatabaseName { get; set; }

        bool DeleteSchemaOnDeletePlan { get; set; }

        bool ResetLastSchemaOnDeleteSchema { get; set; }

        #endregion

        #region Style

        string Style { get; set; }

        #endregion
    }

    public class OptionsDataAccess : BaseDataAccess, IOptionsDataAccess
    {
        public OptionsDataAccess(ISettingsProvider settings)
            : base(settings)
        {
        }

		#region DisplaySettings
		
		public bool ShowDaysInPlan
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
						return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.DisplayOptions, 1) == 1;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return false;
                }
			}
			set
			{
				using (new WaitCursor())
				{
					try
					{
						DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.DisplayOptions, 1, value ? 1 : 0);
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}
				}
			}
        }

    	public bool ShowPensumInPlan
    	{
    		get
    		{
				using (new WaitCursor())
				{
					try
					{
						return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.DisplayOptions, 2) == 1;
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}

					return false;
				}
			}
			set
			{
				using (new WaitCursor())
				{
					try
					{
						DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.DisplayOptions, 2, value ? 1 : 0);
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}
				}
			}
    	}

		public bool ColorWeekendInPlan
		{
			get
			{
				using (new WaitCursor())
				{
					try
					{
						return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.DisplayOptions, 3) == 1;
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}

					return false;
				}
			}
			set
			{
				using (new WaitCursor())
				{
					try
					{
						DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.DisplayOptions, 3, value ? 1 : 0);
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}
				}
			}
		}

        public bool ShowPrsId
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.DisplayOptions, 4) == 1;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return false;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.DisplayOptions, 4, value ? 1 : 0);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        public enNameDisplay NameDisplayOption
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        var i = (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.DisplayOptions, 5);
                        return (enNameDisplay)i;

                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return 0;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.DisplayOptions, 5, (int)value);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

		#endregion

		#region Colors

		public Color DienstCountPlusColor
        {
            get
    		{
				using (new WaitCursor())
				{
					try
					{
                        var color = ((int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.DienstGridColor, 1)).ToColor();
                        return color != null ? (Color)color : Colors.Transparent;
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}

					return Colors.Transparent;
				}
			}
			set
			{
				using (new WaitCursor())
				{
					try
					{
						DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.DienstGridColor, 1, value.ToInt(), value.ToArgbString());
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}
				}
			}
        }

        public Color DienstCountEnoughColor
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        var color = ((int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.DienstGridColor, 2)).ToColor();
                        return color != null ? (Color)color : Colors.Transparent;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return Colors.Transparent;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.DienstGridColor, 2, value.ToInt(), value.ToArgbString());
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        public Color DienstCountMinusColor
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        var color = ((int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.DienstGridColor, 3)).ToColor();
                        return color != null ? (Color)color : Colors.Transparent;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return Colors.Transparent;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.DienstGridColor, 3, value.ToInt(), value.ToArgbString());
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        public Color SaturdayColor
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        var color = ((int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.GridColor, 1)).ToColor();
                        return color != null ? (Color)color : Colors.Transparent;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return Colors.Transparent;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.GridColor, 1, value.ToInt(), value.ToArgbString());
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        public Color SundayColor
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        var color = ((int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.GridColor, 2)).ToColor();
                        return color != null ? (Color)color : Colors.Transparent;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return Colors.Transparent;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.GridColor, 2, value.ToInt(), value.ToArgbString());
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
		}

		public Color HolidayColor
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        var color = ((int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.GridColor, 3)).ToColor();
                        return color != null ? (Color)color : Colors.Transparent;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return Colors.Transparent;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.GridColor, 3, value.ToInt(), value.ToArgbString());
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
		}
		
		#endregion

		#region Work time settings

		public double WorkTimePerDay
		{
			get
			{
				using (new WaitCursor())
				{
					try
					{
						var time = DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.Arbeitszeitprotag, 0);
						return time;
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}

					return 0;
				}
			}
			set
			{
				using (new WaitCursor())
				{
					try
					{
						DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.Arbeitszeitprotag, 0, value);
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}
				}
			}
		}

		public double WorkTimePerWeek
		{
			get
			{
				using (new WaitCursor())
				{
					try
					{
						var time = DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.Arbeitszeitprowoche, 0);
						return time;
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}

					return 0;
				}
			}
			set
			{
				using (new WaitCursor())
				{
					try
					{
						DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.Arbeitszeitprowoche, 0, value);
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}
				}
			}
		}

		public double HolydaysPerYear
		{
			get
			{
				using (new WaitCursor())
				{
					try
					{
						var time = DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.Ferientage, 0);
						return time;
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}

					return 0;
				}
			}
			set
			{
				using (new WaitCursor())
				{
					try
					{
						DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.Ferientage, 0, value);
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}
				}
			}
		}

        #endregion

        #region Printing

        public bool PrintSaldoDifference
    	{
    		get
    		{
				using (new WaitCursor())
				{
					try
					{
						return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.PrintOptions, 1) == 1;
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}

					return false;
				}
			}
			set
			{
				using (new WaitCursor())
				{
					try
					{
						DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.PrintOptions, 1, value ? 1 : 0);
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}
				}
			}
    	}

		public bool PrintSoll
    	{
    		get
    		{
				using (new WaitCursor())
				{
					try
					{
						return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.PrintOptions, 2) == 1;
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}

					return false;
				}
			}
			set
			{
				using (new WaitCursor())
				{
					try
					{
						DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.PrintOptions, 2, value ? 1 : 0);
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}
				}
			}
    	}

        public bool PrintHaben
    	{
    		get
    		{
				using (new WaitCursor())
				{
					try
					{
						return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.PrintOptions, 3) == 1;
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}

					return false;
				}
			}
			set
			{
				using (new WaitCursor())
				{
					try
					{
						DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.PrintOptions, 3, value ? 1 : 0);
					}
					catch (Exception e)
					{
						//TODO: show a messagebox here with the error
						Console.Write(e);
					}
				}
			}
    	}

		#endregion

        #region Person Overview

        //if ((int)Option.NumberValue("PersonDisplaOptions", 1) == 1) { check = true; } else { check = false; }
        //    checkShowAbteilungsName.Checked = check;

        public bool ShowAbteilungName
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.PersonDisplaOptions, 1) == 1;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return false;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.PersonDisplaOptions, 1, value ? 1 : 0);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        //    if ((int)Option.NumberValue("PersonDisplaOptions", 2) == 1) { check = true; } else { check = false; }
        //    checkShowMonth.Checked = check;
        public bool ShowMonth
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.PersonDisplaOptions, 2) == 1;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return false;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.PersonDisplaOptions, 2, value ? 1 : 0);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        //    if ((int)Option.NumberValue("PersonDisplaOptions", 3) == 1) { check = true; } else { check = false; }
        //    checkShowSaldo.Checked = check;
        public bool ShowSaldo
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.PersonDisplaOptions, 3) == 1;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return false;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.PersonDisplaOptions, 3, value ? 1 : 0);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        //    if ((int)Option.NumberValue("PersonDisplaOptions", 4) == 1) { check = true; } else { check = false; }
        //    checkPlanName.Checked = check;
        public bool ShowPlanName
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.PersonDisplaOptions, 4) == 1;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return false;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.PersonDisplaOptions, 4, value ? 1 : 0);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        #endregion

        #region General

        public IEnumerable<Mandant> GetMandanten()
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.CommonService.GetMandanten(Settings.DatabaseSettings);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return null;
            }
        }

        public bool SetActiveMandant(string mandid)
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.CommonService.SetActiveMandant(Settings.DatabaseSettings, mandid);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return false;
            }
        }

        public string GetActiveMandid()
        {
            using (new WaitCursor())
            {
                try
                {
                    return DataServices.CommonService.GetMandid(Settings.DatabaseSettings);
                }
                catch (Exception e)
                {
                    //TODO: show a messagebox here with the error
                    Console.Write(e);
                }

                return "";
            }
        }

        public bool SavePassword
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        var login = LoginData.Deserialize();

                        if (login == null)
                            return false;

                        return login.SavePassword;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return false;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        var login = LoginData.Deserialize();

                        if (login == null)
                            login = new LoginData();

                        login.SavePassword = value;
                        LoginData.Serialize(login);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        public bool ShowDatabaseName
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.GeneralOptions, 1) == 1;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return false;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.GeneralOptions, 1, value ? 1 : 0);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        public bool DeleteSchemaOnDeletePlan
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.GeneralOptions, 2) == 1;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return false;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.GeneralOptions, 2, value ? 1 : 0);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        public bool ResetLastSchemaOnDeleteSchema
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        return (int)DataServices.OptionsService.NumberValue(Settings.DatabaseSettings, enOptions.GeneralOptions, 3) == 1;
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return false;
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.GeneralOptions, 3, value ? 1 : 0);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
        }

        #endregion

        #region Style

        public string Style
        {
            get
            {
                using (new WaitCursor())
                {
                    try
                    {
                        return DataServices.OptionsService.StringValue(Settings.DatabaseSettings, enOptions.Style, 1);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }

                    return "Dark";
                }
            }
            set
            {
                using (new WaitCursor())
                {
                    try
                    {
                        DataServices.OptionsService.SaveData(Settings.DatabaseSettings, enOptions.Style, 1, 0, value);
                    }
                    catch (Exception e)
                    {
                        //TODO: show a messagebox here with the error
                        Console.Write(e);
                    }
                }
            }
		}
        
        #endregion
    }
}
